package com.leyou.cart.controller;

import com.leyou.cart.pojo.Cart;
import com.leyou.cart.sevice.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

/**
 * @Description:
 * @author: Mr_Sun
 * @Date: 2018/9/26
 * @Time: 14:46
 */
@Controller
public class CartController {
    @Autowired
    private CartService cartService;

    @PostMapping
    public ResponseEntity<Void>  addCart(@RequestBody  Cart cart) { //传递过来的是一个jason对象
        this.cartService.addCart(cart);
        return ResponseEntity.status(HttpStatus.CREATED).build();

    }

    /**
     * 登录状态下查询用户的购物车
     * @return
     */
    @GetMapping
    public ResponseEntity<List<Cart>>  queryCarts() {
        List<Cart> carts=  this.cartService.queryCarts();
        if (CollectionUtils.isEmpty(carts)) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(carts);

    }
}
